#pragma once

#include "Joystick.h"
#include <direct.h>
#include "qobject.h"
#include "qvector.h"

class DeviceManager :
	public QObject
{
	Q_OBJECT
public:
	DeviceManager(void);
	~DeviceManager(void);

	void init();
	QString getDeviceName(int id);
	int getGamepadCount();
	void select(int devId);
	void pollDevices();

public slots:
	void onAxisChanged(int axis, int value) { emit axisChanged(axis, value); }
	void onKeyDown(int keyCode) { emit keyDown(keyCode); }
	void onKeyUp(int keyCode) { emit keyUp(keyCode); }

signals:
	void axisChanged(int axis, int value);
	void keyDown(int keyCode);
	void keyUp(int keyCode);

private:
	static int CheckConnectedDevices(LPCDIDEVICEINSTANCE device, void *thisPtr);
	QVector<LPCDIDEVICEINSTANCE> _devices;
	IDirectInput8 *_api;
	int _selectedDeviceIndex;
	Joystick *_selectedDevice;
};

class DeviceManagerException {
	int _code;
public:
	DeviceManagerException(int code) : _code(code) {}
	~DeviceManagerException(){}

	int getCode() { return _code; } 
};

class DeviceManagerInitException :
	public DeviceManagerException 
{
public:
	DeviceManagerInitException(int code) : DeviceManagerException(code) {}
	~DeviceManagerInitException(){}
};
